export default {

	methods: {
		//工单-上门
		async WoFieldServiceAsync(WOFieldService, token) {
			let result = await this.workorderdetailservice.woFieldServiceAsync(WOFieldService, token);
			console.log(result);
			if (result.RetBool == true) {
				uni.showToast({
					title: "已确定上门",
					icon: "none"
				});
				uni.hideLoading();
				this.$refs.visitPoPup.closePoPup();
				this.onLoadData();
			} else {
				uni.hideLoading();
				uni.showToast({
					title: result.RetMsg,
					icon: "none"
				});
			}
			uni.hideLoading();
		},

		//预约
		async OrderReservationAsync(AppointmentTime, Remark, ReasonCodeId) {
			let result = await this.workorderdetailservice.WOAppointmentAsync(this.UserInfo.mobile, this.orderDetailId,
				AppointmentTime, Remark,
				ReasonCodeId, this.UserInfo.RetId);
			console.log(result);
			if (result.RetBool == true) {
				uni.showToast({
					title: "已成功预约",
					icon: "none"
				});
				this.$refs.reservationPoPup.closePoPup();
				this.onLoadData();
			} else {
				uni.showToast({
					title: result.RetMsg,
					icon: "none"
				});
			}
			uni.hideLoading();
		},

		//获取工单详情
		async GetOrderDetailData(WoGuid, callback) {
			let userPhone = this.UserInfo.mobile;
			let token = this.UserInfo.RetId;
			let result = await this.workorderdetailservice.woItemAsync(userPhone, WoGuid, token);
			if (result.RetBool == true) {
				let data = JSON.parse(result.RetObject);
				this.orderDetailData = data;
				console.log(this.orderDetailData);

			} else {
				uni.showToast({
					title: result.RetMsg,
					icon: "none"
				});
			}
			if (callback && typeof callback == "function") {
				callback();
			}
			uni.hideLoading();

		},


		//根据经纬度查出地址名称
		async getLocationAddress(lon, lat) {
			let params = {
				location: `${lat},${lon}`
			}
			let result = await this.mapApiService
				.geoMultipParamMapAsync(this.qqMapServiceUrl, "/ws/geocoder/v1", params);
			return result;
		},

		//地址转经纬度
		async getAddressMapAsync(address) {
			let result = await this.mapApiService.geoCommMapAsync(this.qqMapServiceUrl,
				"/ws/geocoder/v1",
				"address",
				address);
			return result;
		},

		//根据经纬度计算距离
		async calcMapDistance(currentLongitude, currentLatitude, accountLongitude, accountLatitude) {
			let form = new Array(currentLatitude, currentLongitude);
			let to = new Array(accountLatitude, accountLongitude);
			let params = {
				mode: "driving",
				from: form.toString(),
				to: to.toString(),
			};
			let {
				status,
				result
			} = await this.mapApiService.geoMultipParamMapAsync(this.qqMapServiceUrl, "/ws/distance/v1", params);
			console.log(result);
			if (status == 0) {
				//距离-米
				let distance = result.elements[0].distance;

				return distance / 1000;

			}
			return 0;
		},

		//根据地址名称获取经纬度
		async getLonglatByGPS(address, token) {
			let {
				status,
				result
			} = await this.getAddressMapAsync(address);
			if (status == 0) {
				//console.log(result);
				this.accountLongitude = result.location.lng;
				this.accountLatitude = result.location.lat;

				this.VisitData.accountLongitude = result.location.lng;
				this.VisitData.accountLatitude = result.location.lat;
				this.VisitData.IsLocationSuccess = true;

				// let distance = await this.calcMapDistance(this.currentLongitude, this.currentLatitude,
				// 	this.accountLongitude, this.accountLatitude);
				// return distance;
			}
			//else
			// 	return 0;
		},

		async setLocationDistance(res) {
			this.currentLongitude = res.longitude;
			this.currentLatitude = res.latitude;
			this.VisitData.currentLongitude = res.longitude;
			this.VisitData.currentLatitude = res.latitude;
			let result = await this.getLocationAddress(this.currentLongitude, this.currentLatitude);
			this.VisitData.Location = result.result.address;
			// console.log(`me.userPosition${me.userPosition}`)
			// console.log(`me.orderDetailData ${me.orderDetailData}`)
			this.getLonglatByGPS(this.orderDetailData.DisplayAdress,
				this.UserInfo.RetId);
			//this.VisitData.Milestone = distance
			console.log(this.VisitData.Milestone);

			uni.hideLoading()
		},
		//上门组件打开时
		VisitPoPupOpen() {
			uni.showLoading({
				title: "打开...",
				mask: true
			})
			this.$refs.visitPoPup.openPoPup();
		},

		
		GetUserLocation(callback) {
			//debugger
			let me = this;
			this.VisitData.IsLocationSuccess = false;
			uni.getLocation({
				type: 'gcj02',
				success: (res) => {
					this.setLocationDistance(res)
				},
				fail: (res) => {
					if (res.errMsg == "getLocation:fail auth deny") {
						uni.showModal({
							title: '是否授权当前位置',
							content: '需要获取您的地理位置，请确认授权',
							success: (tip) => {
								if (tip.confirm) {
									uni.openSetting({
										success: (data)=> {
											if (data.authSetting["scope.userLocation"] === true) {
												uni.getLocation({
													type: 'gcj02',
													success: (res)=> {
														this.setLocationDistance(res)
													}
												});
											}
										},
										fail: (error)=> {
											uni.hideLoading();
											wx.showToast({
												title: '调用授权窗口失败',
												icon: 'none',
												duration: 1000
											})
										}
									})
								}else{
									uni.hideLoading();
								}
							}
						})
					}
				}
			});
		
		},

		//工单接单
		async AcceptorderAsync(WoId) {
			console.log(WoId);
			let result = await this.workorderlistervice.WOTakingAsync(this.UserInfo.mobile, WoId, this.UserInfo.RetId);
			console.log("接单接口：" + result);
			if (result.RetBool == true) {
				uni.showToast({
					title: "接单成功",
					icon: "none",
					duration:2000
				});
				this.onLoadData();
			} else {
				uni.showToast({
					title: result.RetMsg,
					icon: "none"
				});
			}
			uni.hideLoading();
		},
		//生成配件行
		async CreateAssemblyBom(WoId) {
			uni.showLoading({
				mask: true
			});
			debugger;
			let result = await this.workorderdetailservice.CreateAssemblyBomAsync(this.UserInfo.mobile, WoId, this.UserInfo.RetId)
			if (!result) {
				uni.showToast({
					title: "生成配件行失败!",
					icon: "none"
				});
				uni.hideLoading();
				return;
			}
			if (result.RetBool == true) {
				uni.showToast({
					title: "已生成配件行",
					icon: "none"
				});
			} else {
				uni.showToast({
					title: result.RetMsg,
					icon: "none"
				});
			}
			uni.hideLoading();
		},
		//咨询完工
		async ConsultEndAsync(data) {
			this.$refs.consultEndPopup.closePoPup();
			let result = await this.workorderdetailservice.WOConsultFinishAsync(data, this.UserInfo.RetId);
			if (result) {
				if (result.RetBool == true) {
					uni.showToast({
						title: "咨询完工成功",
						icon: "none"
					});
					this.onLoadData();
				} else {
					uni.showToast({
						title: "咨询完工失败" + result.RetMsg,
						icon: "none"
					});
				}
			} else {
				uni.showToast({
					title: "咨询完工失败",
					icon: "none"
				});
			}
			uni.hideLoading();
		},
		//返工
		async ReworkAsync(Remark, ReasonId) {
			let result = await this.workorderdetailservice.WOReworkAsync(this.UserInfo.mobile, this.orderDetailData.WoId,
				ReasonId, Remark, this.UserInfo.RetId);
			this.$refs.reworkPopup.closePoPup();
			if (result) {
				if (result.RetBool == true) {
					uni.showToast({
						title: "返工成功",
						icon: "none"
					});
					this.onLoadData();
				} else {
					uni.showToast({
						title: result.RetMsg,
						icon: "none"
					});
				}
			} else {
				uni.showToast({
					title: "返工失败",
					icon: "none"
				});
			}
			uni.hideLoading();
		},


		//催单处理
		async WOPushAsync(Remark) {
			let result = await this.workorderdetailservice.WOPushAsync(this.UserInfo.mobile, this.orderDetailId, Remark, this.UserInfo
				.RetId);
			console.log(result);
			if (result.RetBool == true) {
				this.$refs.warnOrderPopup.closePoPup();
				uni.showToast({
					title: "催单成功",
					icon: "none"
				});
				this.onLoadData();
			} else {
				uni.showToast({
					title: "催单失败",
					icon: "none"
				});
			}
			uni.hideLoading();
		},


		//更新工单
		async WOUpdateOrderAsync(updateOrderData, token) {
			let result = await this.workorderdetailservice.WOUpdateOrderAsync(updateOrderData, token);
			console.log(result);
			if (result.RetBool == true) {
				this.$refs.warnOrderPopup.closePoPup();
				uni.showToast({
					title: "更新工单成功",
					icon: "none"
				});
				this.$refs.upWorderPopup.closePoPup();
				this.onLoadData();
			} else {
				uni.showToast({
					title: "更新工单失败",
					icon: "none"
				});
			}
			uni.hideLoading();
		},		
	}
}
